require(['./config'], () => {
    require(['header','footer'], () => {
        class register {
            constructor () {
                this.btnClick()
                this.agreeChange()
            }
            agreeChange () {
                $('.agreebtn').on('change', function () {
                    if ($('.agreebtn').prop('checked')) {
                        $('#register-btn').addClass('ac')
                    }
                })
            }
            btnClick () {
                $("#register-btn").on('click', function () {
                    const name = $('#username').val()
                    const userpwd = $('#userpwd').val()
                    const user = {
                        name: name,
                        pwd: userpwd
                    }
                    if (name&&userpwd) {
                       let msg = localStorage.getItem('usermsg')
                        if (msg) {
                            // localstorage里面有数据
                            msg = JSON.parse(msg)
                            if (msg.some(shop => shop.name === name)) {
                                alert("该昵称已被占用")
                            } else {
                                msg.push(user)
                                localStorage.setItem('usermsg', JSON.stringify(msg))
                                alert("注册成功, 即将跳转登录页")
                                location.href='/html/login.html'
                            }
                        } else {
                            localStorage.setItem('usermsg', JSON.stringify([ user ]))
                            alert("注册成功, 即将跳转登录页")
                            location.href='/html/login.html'
                        }
                    } else {
                        alert("用户名和密码不能为空")
                    } 
                })
            }
        }   
        new register()
    })
})